package cn.team.controller;

import cn.team.entity.Friend;
import cn.team.entity.FriendInfo;
import cn.team.entity.User;
import cn.team.service.FriendService;
import cn.team.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;

@RestController
@RequestMapping("/friend")
public class FriendController {
    @Resource
    FriendService friendService;
    @Resource
    UserService userService;

    @RequestMapping("/getAllFriends")
    public ArrayList<FriendInfo> getAllFriend(@RequestParam("username") String username) {
        ArrayList<Friend> allFriends = friendService.getAllFriends(userService.getUser(username).getId());
        System.out.println(allFriends);
        ArrayList<FriendInfo> allFriendsInfo = new ArrayList<>();
        allFriends.forEach(friend -> {
            FriendInfo info = new FriendInfo();
            System.out.println(friend);
            User user = userService.getUserById(friend.visitorId);
            info.setPortraitName(user.getPortraitName());
            info.setPetName(user.getPetName());
            info.setPersonalSignature(user.getPersonalSignature());
            info.setUsername(user.getUsername());
            info.setIsGroup(user.getIsGroup());
            allFriendsInfo.add(info);
            System.out.println(info);
        });
        return allFriendsInfo;
    }
}